home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Prefs / MPPSaver.c < prev    next >
C/C++ Source or Header  |  1998-08-06  |  7KB  |  239 lines

  1. #include "/MPFiles.h"
  2. #include <math.h>
  3.  
  4. /* Vars in ModePro.c */
  5. extern struct MPSem   *MPSem;
  6. extern UWORD   defaultpens[];
  7. extern WORD    NumDriPens;
  8. extern struct  FileRequester *FileReq;
  9. extern struct  Window *Win;
  10. extern UBYTE   filename[513];
  11. extern struct  TextAttr *TAttr;
  12.  
  13. LONG WCB(LONG Error, struct IFFHandle *iff,APTR a,LONG l);
  14. LONG WriteString(LONG Error, struct IFFHandle *IFF,STRPTR Str);
  15.  
  16. #define FLAG(x,y) (x?y:0)
  17.  
  18. BOOL SaveFile(struct List *List, UBYTE *fname)
  19. {
  20.   struct Promo pm;
  21.   struct IFFHandle *iff;   
  22.   ULONG len,l;
  23.   LONG error;
  24.   UWORD font;
  25.   ULONG flags;
  26.   struct DefaultNode *n;
  27. //  APTR oldwin;
  28.   
  29.   SetIoErr(0);
  30.   
  31.   error=1;
  32.   if(iff=AllocIFF())
  33.   { 
  34.     if(iff->iff_Stream = Open (fname,MODE_NEWFILE))
  35.     {
  36.       InitIFFasDOS(iff);
  37.       if( !(error=OpenIFF(iff,IFFF_WRITE)) )
  38.       {
  39.         if(!(error=PushChunk(iff,ID_PREF,ID_FORM, IFFSIZE_UNKNOWN)))
  40.         {
  41.           for(l=0;l<NUM_LISTS && error==0 ;l++)
  42.           {
  43.             n=(struct DefaultNode *)List[l].lh_Head;
  44.             while(n->Def_Node.ln_Succ && !error)
  45.             {
  46.               if(!(error=PushChunk(iff,ID_PREF,ID_PRMO, IFFSIZE_UNKNOWN)))
  47.               {
  48.                 len=(strlen(n->Def_Node.ln_Name)+2)&0xfffffffe;
  49.                 pm.Version      =9;  //  <---- Muy importante
  50.                 pm.Type         =l;
  51.                 pm.ModeSelect   =n->ModeSelect;
  52.                 pm.ModeID       =n->ModeID;
  53.                 pm.Width        =n->Width;
  54.                 pm.Height       =n->Height;
  55.                 pm.OverscanType =n->OverscanType;
  56.                 pm.AutoScroll   =n->AutoScroll;
  57.                 pm.Look3D       =n->Look3D;
  58.                 pm.NameLen      =len;
  59.                 error=WCB(error,iff,&pm,sizeof(struct Promo));
  60.                 error=WriteString(error,iff,n->Def_Node.ln_Name);
  61.                 if(n->Look3D)
  62.                 {
  63.                   len=DRIPENS;
  64.                   error=WCB(error,iff,&len,4);
  65.                   error=WCB(error,iff,n->Pens,len*sizeof(n->Pens[0]));
  66.                 }
  67.                 else
  68.                 {
  69.                   len=0;
  70.                   error=WCB(error,iff,&len,4);
  71.                 }
  72.                 
  73.                 /** Additions in Format 2 **/
  74.                 flags=n->Flags;
  75.                 error=WCB(error,iff,&flags,sizeof(flags));
  76.  
  77.                 font=n->FontType;
  78.                 error=WCB(error,iff,&font,sizeof(font));
  79.                 if(n->FontType==SFONT_MP)
  80.                 {
  81.                   error=WriteString(error,iff,n->Font.tta_Name);
  82.                   error=WCB(error,iff,&n->Font.tta_YSize,4);
  83.                 }
  84.                 /*** Version 3 ***/
  85.                 error=WCB(error,iff,&n->PubOptions,4);
  86.                 error=WriteString(error,iff,FLAG(n->PubOptions,n->PubName));
  87.                 /*** Version 4 ***/  
  88.                 error=WCB(error,iff,(APTR)&n->Depth,2);
  89.                 error=WriteString(error,iff,FLAG(n->Flags & SHAREPENS,n->LockedPens));
  90.                 /*** Version 5 ***/
  91.                 error=WriteString(error,iff,FLAG(n->Flags & SCRHOTKEY,n->HotKey));  
  92.  
  93.                 /*** Version 6 ***/
  94.                 error=WCB(error,iff,&n->OrigDisplayID,sizeof(ULONG));
  95.                 //printf("%8x\n",n->OrigDisplayID);
  96.                 /*** Version 7 ***/
  97.                 error=WCB(error,iff,&n->ODFlags,sizeof(UBYTE)*2);
  98.                 if((n->Flags & DNF_PALETTE) && n->Colors && n->Palette)
  99.                 {
  100.                   error=WCB(error, iff,&n->Colors,sizeof(ULONG));
  101.                   error=WCB(error, iff,n->Palette,sizeof(struct RGB)*n->Colors);
  102.                 }
  103.                 else
  104.                 {
  105.                   ULONG colors=0;
  106.                   
  107.                   error=WCB(error, iff,&colors,sizeof(ULONG));
  108.                 }
  109.                 /*** Version 8 ***/
  110.                 {
  111.                   error=WCB(error, iff, &n->BackdropType,sizeof(UWORD)); // also writes BackdropFlags
  112.                   error=WriteString(error,iff,(n->BackdropFlags?n->BackdropFile:0));  
  113.                 }
  114.                 /*** Version 9 ***/
  115.                 {
  116.                   LONG bdsize;
  117.                   switch(n->BackdropType)
  118.                   {
  119.                     case 2:
  120.                       bdsize=(sizeof(n->BackdropRGB));
  121.                       error=WCB(error, iff, &bdsize, sizeof(bdsize)); 
  122.                       error=WCB(error, iff, &n->BackdropRGB,sizeof(struct RGB)); // also writes BackdropFlags
  123.                       break;
  124.                     default:
  125.                       bdsize=0;
  126.                       error=WCB(error, iff, &bdsize, sizeof(bdsize)); 
  127.                       break;
  128.                   }
  129.                 }
  130.                 
  131.                 /**** End of Data ***/
  132.                 n=(struct DefaultNode *)(n->Def_Node.ln_Succ);
  133.                 error|=PopChunk(iff);
  134.               }
  135.             }/* wend */           
  136.             
  137.           }/* end l */
  138.           if(!(error=PushChunk(iff,ID_PREF,ID_MPPR, IFFSIZE_UNKNOWN)))
  139.           {
  140.             struct MPPR_Chunk c;
  141.               
  142.             c.Version         =1;
  143.             c.EnablePromotion =MPSem->Enabled;
  144.             c.EnableNLM       =MPSem->EnabledNLM;
  145.             c.AskUnknown      =MPSem->CatchScreens;
  146.             c.AskUnknownPub   =MPSem->CatchPubScreens;
  147.             c.CenterUnknown   =MPSem->CenterAll;
  148.  
  149.             error=WCB(error,iff,&c,sizeof(c));
  150.             error|=PopChunk(iff);
  151.           }
  152.           error|=PopChunk(iff);
  153.         }
  154.         CloseIFF(iff);
  155.       }
  156.       Close(iff->iff_Stream);
  157.     }
  158.     FreeIFF(iff);
  159.   }
  160.   if(error<0)
  161.     return(FALSE);
  162.   else
  163.     return(TRUE);
  164. }
  165.  
  166.  
  167. LONG WCB(LONG Error, struct IFFHandle *iff,APTR a,LONG l)
  168. {
  169.   LONG error=0;
  170.   
  171.   if(Error>=0)
  172.   {
  173.     error=WriteChunkBytes(iff,a,l);
  174.     if(error==l) error=0;
  175.   }
  176.   return(error);
  177. }
  178.  
  179. LONG WriteString(LONG Error, struct IFFHandle *IFF,STRPTR Str)
  180. {
  181.   LONG error=0,len;
  182.   WORD Null=0;
  183.  
  184.   if(Error>=0)
  185.   {
  186.     if(Str)
  187.     {
  188.       len=strlen(Str)+1;
  189.       error=WCB(error,IFF,Str,len);
  190.       if(!error && (len & 1))
  191.       {
  192.         error=WCB(error,IFF,&Null,1); /* WORD align */
  193.       } 
  194.     }
  195.     else
  196.       error=WCB(error,IFF,&Null,2);
  197.   }
  198.    
  199.   return(error);
  200. }
  201.  
  202.  
  203.  
  204. BOOL SelectFile(UBYTE *title,UBYTE save)
  205.   WORD width;
  206.   
  207.   if(!FileReq)
  208.   {
  209.     width=Win->Width/2;
  210.     FileReq=(struct FileRequester *)AllocAslRequestTags(ASL_FileRequest, 
  211.                     ASLFR_TextAttr,        TAttr,
  212.                     ASLFR_InitialTopEdge,  Win->TopEdge+8,
  213.                     ASLFR_InitialLeftEdge, Win->LeftEdge+width/2,
  214.                     ASLFR_InitialWidth,    width,
  215.                     ASLFR_InitialHeight,   Win->Height-16,
  216.                     ASLFR_DoPatterns  ,    TRUE,
  217.                     ASLFR_InitialPattern,  "#?",
  218.                     ASLFR_RejectIcons,     TRUE,
  219.                     TAG_DONE); 
  220.   }
  221.   if(FileReq)
  222.   {
  223.     if(AslRequestTags(FileReq, 
  224.                   ASLFR_Window   ,       Win,
  225.                   ASLFR_SleepWindow ,    TRUE,
  226.                   ASLFR_TitleText,       title,
  227.                   ASLFR_DoSaveMode,      save,
  228.                   TAG_DONE))
  229.     {
  230.       strncpy(filename,FileReq->fr_Drawer,512);
  231.       if(AddPart(filename,FileReq->fr_File,512))
  232.         return(TRUE);
  233.     }   
  234.   }
  235.   return(FALSE);
  236. }
  237.  
  238.